// test1.cpp
// NCC - Copyright (C) 2012 Willow Schlanger <wschlanger@gmail.com>
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list
//    of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
//    of conditions and the following disclaimer in the documentation and/or other materials
//    provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "../util/ncc_util.h"

#include <stdexcept>
#include <iostream>
#include <cstdio>

namespace ncc = NetwideCompilerCollection;

int main()
{
	using namespace std;
	
	FILE *fo = ncc::NccCommonUtils::universalOpen("test1in.txt", "wb");
	fputc('a', fo);
	fputc(13, fo);
	fputc(13, fo);
	fputc(13, fo);
	fputc(10, fo);
	fputc('b', fo);
	fputc(13, fo);
	fputc(10, fo);
	fputc('c', fo);
	fputc(13, fo);
	fputc(13, fo);
	fputc('!', fo);
	fputc(13, fo);
	fputc('d', fo);
	fputc(10, fo);
	fputc('x', fo);
	fputc(13, fo);
	fputc(13, fo);
	fputc(13, fo);
	fputc(13, fo);
	fputc(13, fo);
	fclose(fo);
	
	FILE *fi = ncc::NccCommonUtils::universalOpen("test1in.txt", "rb");
	ncc::NccCFileByteStream is(fi, false, false);
	ncc::NccUtfCharacterSource src1(is, ncc::NccUtfCharacterSource::INPUT_MODE_DEFAULT);
	ncc::NccUtf32CharacterSource src2(src1, true /* fail on invalid characters */);
	ncc::NccNormalizedLineSourceFilter src3(src2);
	ncc::NccUtf8FromUtf32CharacterSource src(src3);
	
	while(src.current() >= 0)
	{
		ncc::S4 chr = src.current();
		if(chr == '\r')
			std::cout << "<CR>" << std::flush;
		else
		if(chr == '\n')
			std::cout << "<LF>\n" << std::flush;
		else
		if(chr <= 127)
			std::cout << (char)(chr) << std::flush;
		else
			std::cout << "<0x" << std::hex << chr << std::dec << ">" << std::flush;
		
		src.next();
	}
	std::cout << std::endl;
	
	if(src.current() != ncc::NCC_STATUS_EOS)
	{
		std::cout << "ERROR: ";
		if(src.status().empty())
			std::cout << "Unknown internal error";
		else
			std::cout << src.status();
		std::cout << std::endl;
		return 1;
	}
	
	ncc::NccSourceQueryObject getLinePurity = src.setupQuery("NccQueryNewLinePurity", ncc::NCC_QUERY_OUTTYPE_PTR_S4);
	ncc::NccSourceQueryObject getLineType = src.setupQuery("NccQueryNewLineType", ncc::NCC_QUERY_OUTTYPE_PTR_S4);
	bool ok = true;
	ncc::S4 result;
	result = -1;
	ok = ncc::nccSourceQuery(&getLinePurity, ncc::NCC_QUERY_OUTTYPE_PTR_S4, &result, 0, 0);
	std::cout << "line purity: " << result << std::endl;
	result = -1;
	ok = ncc::nccSourceQuery(&getLineType, ncc::NCC_QUERY_OUTTYPE_PTR_S4, &result, 0, 0);
	std::cout << "line type: " << result << std::endl;

	return 0;
}
